//
//  LPjson.m
//  LawAndProtect
//
//  Created by 铸功 戚 on 16/1/21.
//  Copyright © 2016年 gw. All rights reserved.
//

#import "HttpMamager.h"
#import <TMT4IOS/AFNetworking.h>
//#import "AFNetworking.h"
@implementation HttpMamager
+(void)staturl:(NSString *)baseUrl path:(NSString *)path params:(NSDictionary *)params statusesWithSuccess:(StatusSuccessBlock)success failure:(StatusFailureBlock)failure method:(NSString *)method
{
     // 启动系统风火轮
 [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
//    AFHTTPRequestOperationManager *manager=[[AFHTTPRequestOperationManager alloc]init];
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"baiduapp/json",@"text/html",@"text/plain",nil];
//    [manager.requestSerializer setValue:@"application/json"forHTTPHeaderField:@"Accept"];
//    [manager.requestSerializer setValue:@"application/json; charset=utf-8"forHTTPHeaderField:@"Content-Type"];
//   NSString *paths =[path stringByAppendingString:@"?"];
   
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *skt=[baseUrl stringByAppendingString:path];
    NSLog(@"%@",skt);
     NSString *encoded = [skt stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    if ([method isEqual:@"POST"]) {
        NSLog(@"%@",params);
        [manager POST:encoded parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            success(responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            success(error);
        }];

    }else{
        [manager GET:encoded parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            success(responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            success(error);
        }];
    }

}
+ (void)getWithPath:(NSString *)path params:(NSDictionary *)params success:(StatusSuccessBlock)success failure:(StatusFailureBlock)failure{

    [self staturl:HTTPS path:path params:params statusesWithSuccess:success failure:failure method:@"GET"];
}
+ (void)postWithPath:(NSString *)path params:(NSDictionary *)params success:(StatusSuccessBlock)success failure:(StatusFailureBlock)failure{

  [self staturl:HTTPS path:path params:params statusesWithSuccess:success failure:failure method:@"POST"];
}
+ (id)synchronizationGet:(NSString *)urlSring {
    NSURL *url=[NSURL URLWithString:urlSring];
    NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    if (data.length>0) {
        NSDictionary *responseDic=[NSJSONSerialization JSONObjectWithData:data  options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@==%@",data,responseDic);
        return responseDic;
        
    }else{
        return  nil;
    }
}



+ (instancetype)sharedHTTPRequestManager {
    static dispatch_once_t token;
    static HttpMamager *instance;
    dispatch_once(&token, ^{
        instance = [[HttpMamager alloc] init];
    });
    return instance;
}


@end
